package com.example.common.enums;

import com.example.common.api.IErrorCode;

/**
 * 错误码
 * @author mie
 */
public enum ResponseCode implements IErrorCode {
    FAILED(-1, "操作失败"),
    SUCCESS(0, "执行成功");

    private final int code;
    private final String msg;

    private ResponseCode(final int code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResponseCode fromCode(int code) {
        ResponseCode[] ecs = values();
        for (ResponseCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return SUCCESS;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", this.code, this.msg);
    }
}
